package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.crm.constant.enums.ProjectTypeEnum;
import com.crm.entity.CrmProject;
import com.crm.model.vo.ProjectReportVo;
import com.crm.model.vo.ReportCountVo;
import com.crm.model.vo.StatisticalCountVo;
import com.crm.service.*;
import com.crm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @author: CRM_SYSTEM
 * @date: 2022/4/7 21:35
 * @Description:
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ICrmProjectService projectService;

    @Autowired
    private ICrmEmployeeProjectService employeeProjectService;
    @Autowired
    private ICrmSiteService siteService;
    @Autowired
    private ICrmArticleService articleService;

    @Override
    public List<ProjectReportVo> projectMonth() {
        List<ProjectReportVo> reportVos = new ArrayList<>();
        List<String> mouths = DateUtils.getStrLastMouth(LocalDate.now(), 11);
        for (ProjectTypeEnum projectTypeEnum : ProjectTypeEnum.values()) {
            ProjectReportVo reportVo = new ProjectReportVo();
            List<ReportCountVo> reportCountVos = new ArrayList<>();
            Map<String, ReportCountVo> countVoMap = null;
            List<ReportCountVo> countVos = projectService.projectMonth(projectTypeEnum.getCode());
            if (CollectionUtils.isNotEmpty(countVos)) {
                countVoMap = countVos.stream().collect(Collectors.toMap(ReportCountVo::getName, Function.identity(), (r1, r2) -> r1));
            }
            for (String mouth : mouths) {
                ReportCountVo countVo = null;
                if (countVoMap != null && countVoMap.containsKey(mouth)) {
                    countVo = countVoMap.get(mouth);
                } else {
                    countVo = new ReportCountVo();
                    countVo.setName(mouth);
                    countVo.setCount(0);
                }
                reportCountVos.add(countVo);
            }
            reportVo.setProjectType(projectTypeEnum.getCode());
            reportVo.setCountVos(reportCountVos);
            reportVos.add(reportVo);
        }
        return reportVos;
    }

    @Override
    public List<ReportCountVo> orderCount() {
        return employeeProjectService.orderCount();
    }

    @Override
    public List<ReportCountVo> projectCount() {
        return projectService.projectCount();
    }

    @Override
    public StatisticalCountVo statisticalCount() {
        StatisticalCountVo statisticalCountVo=new StatisticalCountVo();
//        总场地数目
        int siteCount = siteService.count();
        statisticalCountVo.setSiteCount(siteCount);
//        培训服务数目
        int trainCount = projectService.count(new LambdaQueryWrapper<CrmProject>()
                .eq(CrmProject::getProjectType, ProjectTypeEnum.SPORTS.getCode()));
        statisticalCountVo.setTrainCount(trainCount);

        //        体能次数数目
        int sportsCount = projectService.count(new LambdaQueryWrapper<CrmProject>()
                .eq(CrmProject::getProjectType, ProjectTypeEnum.TRAIN.getCode()));
        statisticalCountVo.setSportsCount(sportsCount);

        int newsCount = articleService.count();
        statisticalCountVo.setNewsCount(newsCount);

        return statisticalCountVo;
    }

    @Override
    public List<ReportCountVo> siteMonthCount() {
        List<ReportCountVo> result=new ArrayList<>();
        List<String> mouths = DateUtils.getStrLastMouth(LocalDate.now(), 11);
        Map<String, ReportCountVo> countVoMap = null;
        List<ReportCountVo> reportCountVos=siteService.siteMonthCount();
        if (CollectionUtils.isNotEmpty(reportCountVos)) {
            countVoMap = reportCountVos.stream().collect(Collectors.toMap(ReportCountVo::getName, Function.identity(), (r1, r2) -> r1));
        }
        for (String mouth : mouths) {
            ReportCountVo countVo = null;
            if (countVoMap != null && countVoMap.containsKey(mouth)) {
                countVo = countVoMap.get(mouth);
            } else {
                countVo = new ReportCountVo();
                countVo.setName(mouth);
                countVo.setCount(0);
            }
            result.add(countVo);
        }
        return result;
    }
}
